package com.kxyu.cash_drawer;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.SecretKeySpec;

/** 
 * DES 加解密 
 */  
public class DesUtils {  
  
    private final static String DES = "DES";  
    private final static String CIPHER_ALGORITHM = "DES/ECB/NoPadding";  
  
    /** 
     * 加密 
     * 
     * @param src 数据源 
     * @param key 密钥，长度必须是8的倍数 
     * @return 返回加密后的数据 
     */  
    public static byte[] encrypt(byte[] src, byte[] key) {  
        SecureRandom sr = new SecureRandom();  
        try {  
            DESKeySpec dks = new DESKeySpec(key);  
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);  
            SecretKey securekey = keyFactory.generateSecret(dks);  
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);  
            cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);  
            return cipher.doFinal(src);  
        } catch (Exception e) {  
        }  
        return null;  
    }  
    
    public static byte[] trides_decrypt(byte data[], byte key[]) {
        try {
            byte[] k = new byte[24];

            int len = data.length;
            if (data.length % 8 != 0) {
                len = data.length - data.length % 8 + 8;
            }
            byte[] needData = null;
            if (len != 0)
                needData = new byte[len];

            for (int i = 0; i < len; i++) {
                needData[i] = 0x00;
            }

            System.arraycopy(data, 0, needData, 0, data.length);

            if (key.length == 16) {
                System.arraycopy(key, 0, k, 0, key.length);
                System.arraycopy(key, 0, k, 16, 8);
            } else {
                System.arraycopy(key, 0, k, 0, 24);
            }
            KeySpec ks = new DESedeKeySpec(k);
            SecretKeyFactory kf = SecretKeyFactory.getInstance("DESede");
            SecretKey ky = kf.generateSecret(ks);

            Cipher c = Cipher.getInstance("DESede/ECB/NOPadding");
            c.init(Cipher.DECRYPT_MODE, ky);
            return c.doFinal(needData);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static byte[] trides_crypt(byte data[], byte key[]) {
        try {
            byte[] k = new byte[24];

            int len = data.length;
            if (data.length % 8 != 0) {
                len = data.length - data.length % 8 + 8;
            }
            byte[] needData = null;
            if (len != 0)
                needData = new byte[len];

            for (int i = 0; i < len; i++) {
                needData[i] = 0x00;
            }

            System.arraycopy(data, 0, needData, 0, data.length);
            if (key.length == 16) {
                System.arraycopy(key, 0, k, 0, key.length);
                System.arraycopy(key, 0, k, 16, 8);
            } else {
                System.arraycopy(key, 0, k, 0, 24);
            }
            SecretKeySpec ky = new SecretKeySpec(k,"DESede");
            Cipher c = Cipher.getInstance("DESede/ECB/NoPadding");
            c.init(Cipher.ENCRYPT_MODE, ky);
            return c.doFinal(needData);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    /** 
     * 生成密钥 
     * 
     * @return 
     * @throws NoSuchAlgorithmException 
     */  
    public static byte[] initKey() throws NoSuchAlgorithmException {  
        KeyGenerator kg = KeyGenerator.getInstance(DES);  
        kg.init(16);  
        SecretKey secretKey = kg.generateKey();  
        return secretKey.getEncoded();  
    }  
  
    /** 
     * 解密 
     * 
     * @param src 数据源 
     * @param key 密钥，长度必须是8的倍数 
     * @return 返回解密后的原始数据 
     */  
    public static byte[] decrypt(byte[] src, byte[] key) {  
        SecureRandom sr = new SecureRandom();  
        try {  
            DESKeySpec dks = new DESKeySpec(key);  
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);  
            SecretKey securekey = keyFactory.generateSecret(dks);  
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);  
            cipher.init(Cipher.DECRYPT_MODE, securekey, sr);  
            return cipher.doFinal(src);  
        } catch (Exception e) {  
        }  
        return null;  
    }  
  
} 